package org.zn.note.designpattern;

/**
 * 访问者模式（Visitor Pattern）
 * 使用访问者类改变元素类的执行算法
 */
public class Pattern25_Visitor {

    public static void main(String[] args) {
        ComputerPart computer = new Keyboard();
        computer.accept(new ComputerPartDisplayVisitor());
    }

    // 元素接口
    private interface ComputerPart {
        void accept(ComputerPartVisitor computerPartVisitor);
    }

    private static class Keyboard implements ComputerPart {
        @Override
        public void accept(ComputerPartVisitor computerPartVisitor) {
            computerPartVisitor.visit(this);
        }
    }

    private static class Monitor implements ComputerPart {
        @Override
        public void accept(ComputerPartVisitor computerPartVisitor) {
            computerPartVisitor.visit(this);
        }
    }

    // 访问者
    private interface ComputerPartVisitor {
        void visit(Keyboard keyboard);

        void visit(Monitor monitor);
    }

    private static class ComputerPartDisplayVisitor implements ComputerPartVisitor {
        @Override
        public void visit(Keyboard keyboard) {
            System.out.println("Displaying Keyboard.");
        }

        @Override
        public void visit(Monitor monitor) {
            System.out.println("Displaying Monitor.");
        }
    }
}
